define((['jquery']), ($) => {
    function detailsLoad() {
        let id = getQueryStr('id') - 0
        function getQueryStr(id) {
            let str = location.search.slice(1)
            let arr = str.split('&')
            let obj = {}
            arr.forEach(item => {
                let newArr = item.split('=')
                obj[newArr[0]] = newArr[1]
            })
            return obj[id]
        }
        //渲染页面
        //渲染当前页面
        $.get({
            url: "/libs/goodsList.json",
            success: (res) => {
                let data = JSON.stringify(res)
                data = JSON.parse(data)
                // console.log(data)
                let result = data.find(item => { return item.product_id == id })
                //渲染数据
                // console.log(result)
                // console.log(typeof(result))

                $('.detailsL_left>img').html(result.images)
                $('.detailsL_right>h2').html(result.name)
                $('.detailsL_right>p').html(result.product_desc_ext)
                $('.detailsL_right>span').html(result.price_min + '元')
                $('.edition_button>ul>li').html(result.value)
                $('.protection>.price>ul>li').html(result.title + '&nbsp;' + '&nbsp;' + '&nbsp;' + result.value)
                $('.protection>.price>ul>.zj').html('总计' + '&nbsp;' + '&nbsp;' + result.price_max + '&nbsp;' + '元')
                
                $('.shopp_Cart').click(function () {
                    // .onclick=(e)=>{
                        // let target=e.target||e.srcElement
                        // if(target.nodeName=='BUTTON'){
                            // let id=target.dataset.id - 0
                            // console.log(id)
                            // console.log(data)
                            let goods = data.find(item=>{return item.product_id == id})
                            console.log(goods)
                            // console.log(list)
                            let list = JSON.parse(localStorage.getItem('cart')) || []
                            // console.log(list)
                            if(!list.length){
                                list.push(goods)
                                goods.cart_num=1
                            }
                            else{
                                let res=list.some(item=>{return item.product_id==id})
                                // console.log(res)
                                if(res){
                                    let index=list.findIndex(item=>{return item.product_id==id})
                                    list[index].cart_num++
                                }else{
                                    list.push(goods)
                                    goods.cart_num=1
                                }
                                
                            }
                            localStorage.setItem('cart',JSON.stringify(list))
                    location.href=('/html/ShoppingCart.html')

                        // }
                    
                    // let outcome=result
                    // var cart = JSON.parse(localStorage.getItem('cart')) || []
                    
                    // cart.cart_number++
                    // cart.push(outcome)
                    
                    // localStorage.setItem('cart',JSON.stringify(cart))
                    
                })
            }

        })
    }
    return {
        detailsLoad: detailsLoad
    }
})